﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.IO;


namespace Paltop_v0._1.UI
{
    /// <summary>
    /// Summary description for AjaxController1
    /// </summary>
    public class AjaxController1 : IHttpHandler, IRequiresSessionState
    {
        private string actionType = "";
        private string action = "";
        private string entity = "";

        public void ProcessRequest(HttpContext context)
        {
            // this line must be deleted 
            //System.Threading.Thread.Sleep(2000);
            try
            {
                
                actionType = context.Request.Form["ACTIONTYPE"];
                action = context.Request.Form["ACTION"];
                int tempId;
                if (actionType != "LOGIN")
                    tempId = int.Parse(context.Session["ACCOUNTID"].ToString());
                switch (actionType.ToUpper())
                {
                    #region Desktop
                    case "DESKTOP":
                        {
                            switch (action.ToUpper())
                            {
                                case "STICKYNOTECREATE":
                                    {
                                        Paltop_.Controllers.StikyNoteController sc = new Paltop_.Controllers.StikyNoteController();

                                        context.Response.Write(sc.StickyNoteCreate(false, int.Parse(context.Session["ACCOUNTID"].ToString())));
                                        break;
                                    }
                                case "STICKYNOTEADD":
                                    {
                                        Paltop_.Controllers.StikyNoteController sc = new Paltop_.Controllers.StikyNoteController();

                                        context.Response.Write(sc.StickyNoteCreate(true, int.Parse(context.Session["ACCOUNTID"].ToString())));
                                        break;
                                    }
                                case "STICKYNOTESAVE":
                                    {
                                        Paltop_.Controllers.StikyNoteController sc = new Paltop_.Controllers.StikyNoteController();
                                        int stikyId = int.Parse(context.Request.Form["SID"]);
                                        string content = context.Request.Form["content"];
                                        context.Response.Write((content == "" ? "" : sc.StickyNoteSave(content, stikyId)));
                                        break;
                                    }
                                case "STICKYNOTERESIZE":
                                    {
                                        Paltop_.Controllers.StikyNoteController sc = new Paltop_.Controllers.StikyNoteController();
                                        int stikyId = int.Parse(context.Request.Form["SID"].Split('_')[1]);
                                        int width = int.Parse(context.Request.Form["width"]);
                                        int height = int.Parse(context.Request.Form["height"]);
                                        context.Response.Write(sc.StickyNoteResized(width, height, stikyId));
                                        break;
                                    }
                                case "STICKYNOTEDRAGED":
                                    {

                                        int xpos = int.Parse(context.Request.Form["XPOS"]);
                                        int ypos = int.Parse(context.Request.Form["YPOS"]);
                                        int stikyId = int.Parse(context.Request.Form["SID"].Split('_')[1]);
                                        Paltop_.Controllers.StikyNoteController stickyController = new Paltop_.Controllers.StikyNoteController();
                                        context.Response.Write(stickyController.StickyNoteDragged(xpos, ypos, stikyId));
                                        break;
                                    }
                                case "STICKYNOTEDELETE":
                                    {
                                        int stikyId = int.Parse(context.Request.Form["SID"]);
                                        Paltop_.Controllers.StikyNoteController stickyController = new Paltop_.Controllers.StikyNoteController();
                                        context.Response.Write(stickyController.StickyNoteDelete(stikyId));
                                        break;
                                    }
                                case "STICKYNOTEMINMAX":
                                    {
                                        string type = context.Request.Form["type"];
                                        Paltop_.Controllers.StikyNoteController stickyController = new Paltop_.Controllers.StikyNoteController();
                                        context.Response.Write(stickyController.StickyNoteMinMax(int.Parse(context.Session["ACCOUNTID"].ToString()), type));
                                        break;

                                    }
                                case "STICKYNOTEMIN":
                                    {
                                        int stikyId = int.Parse(context.Request.Form["SID"]);
                                        Paltop_.Controllers.StikyNoteController stickyController = new Paltop_.Controllers.StikyNoteController();
                                        context.Response.Write(stickyController.StickyNoteMin(int.Parse(context.Session["ACCOUNTID"].ToString()), stikyId));
                                        break;

                                    }
                                case "STICKYNOTECHANGESTYLE":
                                    {
                                        int stikyId = int.Parse(context.Request.Form["SID"]);
                                        Paltop_.Controllers.StikyNoteController stickyController = new Paltop_.Controllers.StikyNoteController();
                                        context.Response.Write(stickyController.StickyChangeStyle(int.Parse(context.Session["ACCOUNTID"].ToString()), stikyId));
                                        break;

                                    }
                                default:
                                    context.Response.Write("Error in AjaxController: Error 3");
                                    break;
                            }
                            break;
                        }

                    #endregion
                    #region LOGIN
                    case "LOGIN":
                        {


                            //System.Threading.Thread.Sleep(5000);
                            switch (action.ToUpper())
                            {
                                //return 1 if mail availabe for registration
                                case "EMAILVARIFY":
                                    {

                                        String email = "";
                                        email = context.Request.Form["EMAIL"];
                                        Paltop_.Controllers.UserControler t = new Paltop_.Controllers.UserControler();
                                        int status = t.CheckMailAvailebility(email);
                                        context.Response.Clear();
                                        context.Response.Write(status);
                                        break;
                                    }
                                case "LOGIN":
                                    {

                                        String email = context.Request.Form["EMAIL"];
                                        String password = context.Request.Form["PASSWORD"];
                                        Paltop_.Controllers.UserControler user = new Paltop_.Controllers.UserControler();
                                        int status = user.LoginCheck(email, password);
                                        if (status == -1)
                                        {

                                            //Wrong Password;
                                            context.Response.Clear();
                                            context.Response.Write("1");

                                        }
                                        else if (status == -2)
                                        {
                                            //Wrong Email
                                            context.Response.Clear();
                                            context.Response.Write("2");

                                        }
                                        else if (status > 0)
                                        {
                                            //Login Accepted
                                            context.Session["ACCOUNTID"] = status;
                                            context.Response.Clear();
                                            context.Response.Write("0");
                                        }
                                        break;
                                    }
                                case "REGISTER":
                                    {
                                        String email = context.Request.Form["EMAIL"];
                                        String password = context.Request.Form["PASSWORD"];
                                        String name = context.Request.Form["NAME"];
                                        Paltop_.Controllers.UserControler register = new Paltop_.Controllers.UserControler();
                                        int status = register.RegisterNewAccount(email, password, name);
                                        if (status == -1)
                                        {

                                            //Wrong Password;
                                            context.Response.Clear();
                                            context.Response.Write("-1");

                                        }

                                        else if (status > 0)
                                        {
                                            //Login Accepted
                                            context.Session.Add("ACCOUNTID", status);
                                            context.Response.Clear();
                                            context.Response.Write("0");
                                        }
                                        else
                                        {
                                            context.Response.Clear();
                                            context.Response.Write(-10);
                                        }
                                        break;
                                    }
                                case "RECOVERPASSWORD":
                                    {
                                        string Email = context.Request.Form["EMAIL"];
                                        Paltop_.Controllers.UserControler user = new Paltop_.Controllers.UserControler();
                                        int status = user.ForgetPassword(Email);
                                        context.Response.Clear();
                                        context.Response.Write(status);
                                        break;
                                    }
                                case "SIZE":
                                    context.Session["WIDTH"] = context.Request.Form["WIDTH"];
                                    context.Session["HIEGHT"] = context.Request.Form["HIEGHT"];
                                    break;


                            }
                            break;
                        }
                    #endregion
                    #region CALENDAR
                    case "CALENDAR":
                        {
                            Paltop_.Controllers.CalendarController cal = new Paltop_.Controllers.CalendarController();
                            switch (action.ToUpper())
                            {
                                case "NEWEVENT":
                                    {
                                        int status;
                                        string data = context.Request.Form["DATA"];
                                        int accountID;
                                        if (context.Session["ACCOUNTID"] != null)
                                        {
                                            accountID = Convert.ToInt32(context.Session["ACCOUNTID"]);
                                        }
                                        else
                                        {

                                            throw new Exception();
                                        }

                                        if (data != null)
                                        {
                                            status = (cal.NewEvent(data, accountID));
                                        }
                                        else
                                        {
                                            status = -1;
                                        }
                                        context.Response.Write(status);
                                        break;
                                    }

                                case "EVENTLIST":
                                    {
                                        String status;
                                        string D = context.Request.Form["DAY"];
                                        string M = context.Request.Form["MOUNTH"];
                                        string Y = context.Request.Form["YEAR"];
                                        int day = Convert.ToInt32(D);
                                        int mounth = Convert.ToInt32(M);
                                        int year = Convert.ToInt32(Y);

                                        int accountID;
                                        if (context.Session["ACCOUNTID"] != null)
                                        {
                                            accountID = Convert.ToInt32(context.Session["ACCOUNTID"]);
                                        }
                                        else
                                        {

                                            throw new Exception();
                                        }
                                        context.Response.Write(cal.GetEventListByDate(accountID, new DateTime(year, mounth, day)));
                                        break;
                                    }

                                case "DELETEEVENT":
                                    {
                                        int eventId = Convert.ToInt32(context.Request.Form["DATA"]);
                                        context.Response.Write(cal.Delete(eventId));
                                        break;
                                    }
                                case "EVENTBYID":
                                    {
                                        int eventId = Convert.ToInt32(context.Request.Form["DATA"]);
                                        context.Response.Write(cal.GetEventbyID(eventId));
                                        break;
                                    }

                                case "EDITEVENT":
                                    {

                                        int status;
                                        string data = context.Request.Form["DATA"];
                                        int accountID;
                                        if (context.Session["ACCOUNTID"] != null)
                                        {
                                            accountID = Convert.ToInt32(context.Session["ACCOUNTID"]);
                                        }
                                        else
                                        {

                                            throw new Exception();
                                        }

                                        if (data != null)
                                        {
                                            status = (cal.EditEvent(data, accountID));
                                        }
                                        else
                                        {
                                            status = -1;
                                        }
                                        context.Response.Write(status);
                                        break;
                                    }
                            }

                            break;
                        }
                    #endregion
                    #region FEEDREADER
                    case "FEED":
                        {
                            switch (action)
                            {
                                case "ADDFEED":
                                    {
                                        Paltop_.Controllers.FeedController a = new Paltop_.Controllers.FeedController();
                                        string url = context.Request.Form["URL"];
                                        var t=a.RssToObject(url);
                                        if (t != null)
                                        {
                                            context.Response.Write("<li class=\"RSSItems\" + id=\""+t.FeedID + "\">" + t.Title + "</li>");
                                        }
                                        else
                                        {
                                            context.Response.Write("0");
                                        }
                                        break;
                                    }
                                case "GETFEED":
                                    {
                                        string id = context.Request.Form["id"];
                                        int FeedId = Convert.ToInt32(id);
                                        Paltop_.Controllers.FeedController a = new Paltop_.Controllers.FeedController();
                                        var t = a.getItemWithID(FeedId);
                                        string output = "";
                                        foreach (var item in t)
                                        {
                                            output= output+ "<li><div class=\"Subject\"><div class=\"SubjectControl\">";
                                            output=output+"<a href="+item.link+">"+item.title+"</a></div></div><div class=\"News_Content\">";
                                            output=output+"<div class=\"ContentControl\">";
                                             output=output+item.body+"</div></div>";
                                            output=output+"<div class=\"Date\">"+item.date+"</div></li>";                                         
                                        }
                                        context.Response.Write(output);
                                        break;
                                    }

                            }
                            break;
                        }
                    #endregion
                    #region FILEMANAGER
                    case "FILEMANAGER":
                        {

                            switch (action)
                            {
                                case "CREATENEWFOLDER":
                                    {
                                        string name = context.Request.Form["Name"];
                                        int pid = int.Parse(context.Request.Form["pid"]);
                                        int userId = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        context.Response.Write(new Paltop_.Controllers.FileManagerController().CreateNewFolder(userId, pid, name));
                                        break;
                                    }
                                case "DELETE":
                                    {
                                        string type = context.Request.Form["type"];
                                        int fid = int.Parse(context.Request.Form["fid"]);
                                        int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        if (new Paltop_.Controllers.FileManagerController().DeleteFileOrFolder(fid, userID, type == "FILE" ? true : false))
                                            context.Response.Write("ok");
                                        else
                                            context.Response.Write("Error in deleting file or folder");
                                        break;
                                    }
                                case "COPY":
                                    {
                                        string type = context.Request.Form["type"];
                                        int fid = int.Parse(context.Request.Form["fid"]);
                                        context.Session["fileMangerCopy"] = fid;
                                        context.Session["fileMangerType"] = type;
                                        context.Response.Write("ok");
                                        break;
                                    }
                                case "CUT":
                                    {
                                        string type = context.Request.Form["type"];
                                        int fid = int.Parse(context.Request.Form["fid"]);
                                        context.Session["fileMangerCut"] = fid;
                                        context.Session["fileMangerType"] = type;
                                        context.Response.Write("ok");
                                        break;
                                    }
                                case "PASTE":
                                    {
                                        int sourceId = 0;

                                        int DestinationId = int.Parse(context.Request.Form["fid"]);
                                        int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        if (context.Session["fileMangerCopy"] != null)
                                        {
                                            try
                                            {
                                                sourceId = int.Parse(context.Session["fileMangerCopy"].ToString());

                                            }
                                            catch (Exception)
                                            {
                                                context.Response.Write("please select a file to copy or cut first");
                                                return;
                                            }
                                            try
                                            {
                                                context.Session["fileMangerCopy"] = null;
                                                Paltop_.Controllers.FileManagerController controller = new Paltop_.Controllers.FileManagerController();

                                                int fileInfoID = controller.CopyPaste(userID, sourceId, DestinationId, context.Session["fileMangerType"].ToString() == "FILE" ? true : false);
                                                if (fileInfoID > 0)
                                                {
                                                    XElement retFolderInfo = controller.GetFileOrFolderInfo(userID, fileInfoID);
                                                    string retVal = "{";
                                                    retVal += "\"Title\":\"" + retFolderInfo.Element("Title").Value + "\",";
                                                    retVal += "\"FileInfoID\":\"" + retFolderInfo.Element("FileInfoID").Value + "\",";
                                                    retVal += "\"ExtentionTitle\":\"" + retFolderInfo.Element("ExtentionTitle").Value + "\",";
                                                    retVal += "\"JSFunction\":\"" + retFolderInfo.Element("JSFunction").Value + "\"}";
                                                    context.Response.Write("ok$" + retVal);
                                                }
                                                else
                                                    context.Response.Write("Error in file copy opetation");

                                            }
                                            catch (Exception ex1)
                                            {
                                                context.Response.Write("Error in file copy opetation");
                                            }
                                        }
                                        else if (context.Session["fileMangerCut"] != null)
                                        {
                                            try
                                            {
                                                sourceId = int.Parse(context.Session["fileMangerCut"].ToString());

                                            }
                                            catch (Exception)
                                            {
                                                context.Response.Write("please select a file to copy or cut first");
                                                return;
                                            }
                                            try
                                            {
                                                context.Session["fileMangerCut"] = null;
                                                Paltop_.Controllers.FileManagerController controller = new Paltop_.Controllers.FileManagerController();
                                                if (controller.CutPaste(userID, sourceId, DestinationId, context.Session["fileMangerType"].ToString() == "FILE" ? true : false))
                                                {
                                                    XElement retFolderInfo = controller.GetFileOrFolderInfo(userID, sourceId);
                                                    string retVal = "{";
                                                    retVal += "\"Title\":\"" + retFolderInfo.Element("Title").Value + "\",";
                                                    retVal += "\"FileInfoID\":\"" + retFolderInfo.Element("FileInfoID").Value + "\",";
                                                    retVal += "\"ExtentionTitle\":\"" + retFolderInfo.Element("ExtentionTitle").Value + "\",";
                                                    retVal += "\"JSFunction\":\"" + retFolderInfo.Element("JSFunction").Value + "\"}";
                                                    context.Response.Write("ok$" + retVal);

                                                }
                                                else
                                                    context.Response.Write("Error in file cut opetation");
                                            }
                                            catch (Exception rer)
                                            {
                                                context.Response.Write("Operation failed!\n");
                                                if (rer.Message.ElementAt(0) == '#')
                                                {
                                                    context.Response.Write(rer.Message.Substring(1));
                                                    return;
                                                }
                                                context.Response.Write("Error in file cut opetation");
                                            }
                                        }
                                        else
                                            context.Response.Write("please select a file to copy or cut first");
                                        break;
                                    }
                                case "CLOSE":
                                    {
                                        context.Session["FORWARDSTACK"] = null;
                                        context.Session["BACKSTACK"] = null;
                                        context.Session["ArgumentHistory"] = null;
                                        context.Response.Write("ok");
                                        break;
                                    }
                                case "OPENTEXT":
                                    {
                                        try
                                        {
                                            Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                            int fileId = int.Parse(context.Request.Form["fid"]);
                                            int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                            string content = fmc.OpenTextFile(fileId, userID);
                                            context.Response.Write(content);
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in retrieving  data");
                                        }

                                        break;
                                    }

                                case "OPENPICTURE":
                                    {
                                        try
                                        {
                                            Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                            int fileId = int.Parse(context.Request.Form["fid"]);
                                            int accountId = int.Parse(context.Session["ACCOUNTID"].ToString());
                                            Paltop_.Controllers.PhotoViewerController p = new Paltop_.Controllers.PhotoViewerController();
                                            var t = p.PhotoList(fileId, accountId);
                                            string addr = "";
                                            List<string> addressList = new List<string>();
                                            foreach (var item in t)
                                            {
                                                addr = "/FileManager/" + accountId;
                                                if (item.ParentID != 0)
                                                {
                                                    var folder = item.Address;

                                                    addr = addr + "/" + folder + "/";
                                                }
                                                else
                                                {
                                                    addr = addr + "/";
                                                }
                                                var title = item.Title;
                                                addr = addr + title;
                                                addressList.Add(addr);
                                            }

                                            JavaScriptSerializer js = new JavaScriptSerializer();
                                            String pAddr = js.Serialize(addressList);
                                            context.Response.Write(pAddr);
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in retrieving  data");
                                        }

                                        break;
                                    }
                                case "OPENOGG":
                                    {
                                        try
                                        {
                                            Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                            int fileId = int.Parse(context.Request.Form["fid"]);
                                            int accountId = int.Parse(context.Session["ACCOUNTID"].ToString());
                                            Paltop_.Controllers.PhotoViewerController p = new Paltop_.Controllers.PhotoViewerController();
                                            var t = p.MovieList(fileId, accountId);
                                            string addr = "";
                                            var item = t;
                                            addr = "/FileManager/" + accountId;
                                            if (item.ParentID != 0)
                                            {
                                                var folder = item.Address;
                                                folder = folder.Substring(1);
                                                addr = addr + "/" + folder + "/";
                                            }
                                            else
                                            {
                                                addr = addr + "/";
                                            }
                                            var title = item.Title;
                                            addr = addr + title;
                                            JavaScriptSerializer js = new JavaScriptSerializer();
                                            String pAddr = js.Serialize(addr);
                                            context.Response.Write(pAddr);
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in retrieving  data");
                                        }

                                        break;
                                    }
                                case "DOWNLOAD":
                                    {
                                        Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                        int fileId = int.Parse(context.Request.Form["fid"]);
                                        int accountId = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        string fileAddress = fmc.GetFile(accountId, fileId);
                                        if (!DownloadFile(fileAddress, context.Response))
                                            context.Response.Write("error");
                                        break;
                                    }
                                case "PROPERTIES":
                                    {
                                        try
                                        {
                                            string type = context.Request.Form["type"];
                                            int fid = int.Parse(context.Request.Form["fid"]);
                                            int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                            Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                            XElement fileInfo = fmc.GetFileOrFolderInfo(userID, fid);
                                            string retVal = "{";
                                            retVal += "\"Address\":\"" + fileInfo.Element("Address").Value.Replace("\\", "=>") + "\",\"Size\":\"" + int.Parse(fileInfo.Element("Size").Value) / 1024 + " kB\",\"CreationDate\":\"" +
                                                fileInfo.Element("CreationDate").Value + "\",\"Type\":\"" + fileInfo.Element("ExtentionTitle").Value + "\"}";
                                            context.Response.Write("ok$" + retVal);
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in retreiving data!");
                                        }
                                        break;
                                    }
                                case "UNZIP":
                                    {
                                        Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                        int fid = int.Parse(context.Request.Form["fid"]);
                                        int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        XElement fileInfo = fmc.GetFileOrFolderInfo(userID, fid);
                                        string addr = "";
                                        string temp = fileInfo.Element("ParentID").Value;
                                        int parentID = Convert.ToInt32(temp);
                                        addr = "/FileManager/" + userID+"/";
                                        if (parentID != 0)
                                        {
                                            var folder = fileInfo.Element("Address").Value;
                                            folder = folder.Substring(1);
                                            addr = addr + "/" + folder + "/";
                                        }
                                        string dest = addr;
                                        dest = context.Server.MapPath(dest);
                                        addr = addr+fileInfo.Element("Title").Value;
                                        string source = addr;
                                        source = context.Server.MapPath(source);
                                        bool res = Paltop_.Controllers.ZipController.UnZipFile(source, dest);
                                        if (File.Exists(source.Replace(".zip", "")))
                                        {
                                            FileInfo fi = new FileInfo(source.Replace(".zip", ""));
                                            fmc.AddFile(userID, fi.Name, fi.Extension.Substring(1), parentID, (int)fi.Length);
                                        }
                                        else
                                        fmc.addFolderToFileManager(parentID, addr.Replace(".zip", ""), userID);
                                        context.Response.Write(res);
                                        break;
                                    }
                                case "ZIP":
                                    {
                                        Paltop_.Controllers.FileManagerController fmc = new Paltop_.Controllers.FileManagerController();
                                        int fid = int.Parse(context.Request.Form["fid"]);
                                        int userID = int.Parse(context.Session["ACCOUNTID"].ToString());
                                        XElement fileInfo = fmc.GetFileOrFolderInfo(userID, fid);
                                        string addr = "";
                                        string temp = fileInfo.Element("ParentID").Value;
                                        int parentID = Convert.ToInt32(temp);
                                        addr = "/FileManager/" + userID + "/";
                                        if (parentID != 0)
                                        {
                                            var folder = fileInfo.Element("Address").Value;
                                            folder = folder.Substring(1);
                                            addr = addr + "/" + folder + "/";
                                        }
                                        addr = addr + fileInfo.Element("Title").Value;
                                        string source = addr;
                                        source = context.Server.MapPath(source);
                                        string dest = addr+".zip";
                                        string title=fileInfo.Element("Title").Value+".zip";
                                        dest = context.Server.MapPath(dest);
                                        bool res = Paltop_.Controllers.ZipController.ZipFolder(source, dest);
                                        FileInfo t=new FileInfo(dest);
                                        long size=t.Length;
                                        if (res)
                                        {
                                            fmc.AddFile(userID, title, "zip", parentID, (int)size);
                                        }
                                        context.Response.Write(res);
                                        break;
                                    }


                                default:
                                    break;
                            }

                            break;
                        }
                    #endregion
                    #region ADMIN
                    case "ADMIN":
                        {
                            switch (action.ToUpper())
                            {
                                case "ADDEXTENTION":
                                    {

                                        try
                                        {
                                            
                                            string jsFunction = context.Request.Form["jsf"];
                                            string title = context.Request.Form["title"].ToLower();
                                            string retVal =Paltop_.Controllers.AdminController.AddNewExtention(jsFunction, title);
                                            context.Response.Write("ok$" + retVal);
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in adding extension");
                                        }

                                        break;
                                    }
                                case "EDITEXTENTION":
                                    {
                                        try
                                        {
                                            string title = context.Request.Form["title"].ToLower();
                                            string jsfunction = context.Request.Form["jsf"];
                                            string decription = context.Request.Form["decription"];
                                            int eid = int.Parse(context.Request.Form["eid"]);
                                            string retVal = Paltop_.Controllers.AdminController.EditExtension(jsfunction, title, decription, eid);
                                            context.Response.Write(retVal);
                                        }
                                        catch (Exception)
                                        {
                                             context.Response.Write("Error in editing extension");
                                        }
                                        break;
                                    }
                                case "DISABLEEXTENTION":
                                    {
                                        try
                                        {
                                            int eid = int.Parse(context.Request.Form["eid"]);
                                            string retVal = Paltop_.Controllers.AdminController.DisableExtension(eid);
                                            context.Response.Write("ok");
                                        }
                                        catch (Exception)
                                        {
                                            context.Response.Write("Error in disabling extension");
                                        }
                                        break;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }
                    # endregion
                    default:
                        context.Response.Write("Error in AjaxController: Error 2");
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("Error in AjaxController: Error 1");
            }
        }

       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private Boolean DownloadFile(string path, HttpResponse response)
        {
            try
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(path);
                    long sz = fi.Length;

                    response.ClearContent();
                    response.ContentType = GetMimeType(System.IO.Path.GetExtension(path));
                    response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(path)));
                    response.AddHeader("Content-Length", sz.ToString("F0"));
                    response.TransmitFile(path);
                    //response.End();
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private static string GetMimeType(string Extension)
        {
            string mime = "application/octetstream";
            if (string.IsNullOrEmpty(Extension))
                return mime;

            string ext = Extension.ToLower();
            Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (rk != null && rk.GetValue("Content Type") != null)
                mime = rk.GetValue("Content Type").ToString();
            return mime;

        }
    }
}
